<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use App\Models\CombustionIntegral;
use App\Models\CombustionRecord;
use App\Models\RewardTransaction;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
//            ->header('康宸生活')
//            ->description('康宸生活')
            ->body(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->row(Dashboard::title());
//                    $column->row(new Examples\Tickets());
                    $column->row(function (Row $row) {
                        $row->column(12, new Examples\Tickets());
                        $this->dataCard($row);
                    });
                });
                $row->column(6, function (Column $column) {
                    $column->row(function (Row $row) {
                        $row->column(6, new Examples\NewUsers());
                        $row->column(6, new Examples\NewDevices());
                    });

                    $column->row(new Examples\Sessions());
                    $column->row(new Examples\ProductOrders());
                });
            });
    }

    private function dataCard($row)
    {
        $rt = RewardTransaction::query()
            ->select('amount', 'change_type', 'transaction_type')
            ->where('change_type', 1)
            ->where('transaction_type', 2);
        $data = collect([
            Card::make('代金券燃烧汇总', CombustionIntegral::query()->select('quantity')->sum('quantity')),
            Card::make('KC通证燃烧汇总', CombustionRecord::query()->select('quantity')->sum('quantity')),
            Card::make('奖励余额分佣汇总', $rt->sum('amount')),
            Card::make('当日分佣汇总', $rt->where('created_at', '>', now()->startOfDay())->sum('amount')),
        ]);

        $xl = 12 / $data->count();
        if ($xl < 2) {
            $xl = 2;
        }
        if (filled($data)) {
            $data->each(function ($item) use ($row, $xl) {
                $row->column($xl, $item);
            });
        }
        return $row;
    }
}
